<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>sizeof</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C/C++ Keywords</a> &gt; <a href=
    "sizeof.html">sizeof</a>
  </div>

  <div class="name-format">
    sizeof
  </div>

  <p>The sizeof operator is a compile-time operator that returns the
  size, in bytes, of the argument passed to it.  For example, the
  following code uses sizeof to display the sizes of a number of
  variables:</p>

  <pre class="example-code">
  struct EmployeeRecord {
    int ID;
    int age;
    double salary;
    EmployeeRecord* boss;
  };

  ...

  cout &lt;&lt; "sizeof(int): " &lt;&lt; sizeof(int) &lt;&lt; endl
       &lt;&lt; "sizeof(float): " &lt;&lt; sizeof(float) &lt;&lt; endl
       &lt;&lt; "sizeof(double): " &lt;&lt; sizeof(double) &lt;&lt; endl
       &lt;&lt; "sizeof(char): " &lt;&lt; sizeof(char) &lt;&lt; endl
       &lt;&lt; "sizeof(EmployeeRecord): " &lt;&lt; sizeof(EmployeeRecord) &lt;&lt; endl;

  int i;
  float f;
  double d;
  char c;
  EmployeeRecord er;

  cout &lt;&lt; "sizeof(i): " &lt;&lt; sizeof(i) &lt;&lt; endl
       &lt;&lt; "sizeof(f): " &lt;&lt; sizeof(f) &lt;&lt; endl
       &lt;&lt; "sizeof(d): " &lt;&lt; sizeof(d) &lt;&lt; endl
       &lt;&lt; "sizeof(c): " &lt;&lt; sizeof(c) &lt;&lt; endl
       &lt;&lt; "sizeof(er): " &lt;&lt; sizeof(er) &lt;&lt; endl;
</pre>

  <p>When run, the above code displays this output:</p>

  <pre class="example-code">
  sizeof(int): 4
  sizeof(float): 4
  sizeof(double): 8
  sizeof(char): 1
  sizeof(EmployeeRecord): 20
  sizeof(i): 4
  sizeof(f): 4
  sizeof(d): 8
  sizeof(c): 1
  sizeof(er): 20
</pre>

  <p>Note that sizeof can either take a variable type (such as
  <strong>int</strong>) or a variable name (such as <strong>i</strong>
  in the example above).</p>

  <p>It is also important to note that the sizes of various types of
  variables can change depending on what system you're on.  Check out
  <a href="../data_types.html">a description
  of the C and C++ data types</a> for more information.</p>

  <p>The parentheses around the argument are not required if you are
  using sizeof with a variable type (e.g. sizeof(int)).</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="../data_types.html">C/C++ Data Types</a>
  </div>

  </div>
  </td>
  


  </tr>
  </table>
</body></html>
